﻿using System;
using System.IO;
using Dexter.Logger.Contracts;
using log4net;
using log4net.Config;

namespace Dexter.Logger.Log4Net {
	public  class Log4NetFactory : ILoggerFactory
	{
		public Log4NetFactory ( )
			: this ( "Configs\\Log4Net.config" )
		{
		}

		public Log4NetFactory(Stream config)
		{
			XmlConfigurator.Configure(config);
		}

		public Log4NetFactory ( string configFile )
		{
			XmlConfigurator.ConfigureAndWatch(GetConfigFile(configFile));
		}

		static FileInfo GetConfigFile ( string fileName ) {
			if (Path.IsPathRooted ( fileName )) {
				return new FileInfo ( fileName );
			}
			var r =  new FileInfo ( Path.Combine ( AppDomain.CurrentDomain.BaseDirectory, fileName ) );

			return r;
		}

		/// <summary>
		/// 	Creates the specified name.
		/// </summary>
		/// <param name = "name">The name.</param>
		/// <returns></returns>
		public ILogger Create ( string name ) {
			return new Log4NetLogger ( LogManager.GetLogger ( name ) );

		}

		/// <summary>
		/// 	Creates the specified type.
		/// </summary>
		/// <param name = "type">The type.</param>
		/// <returns></returns>
		public ILogger Create ( Type type ) {
			return new Log4NetLogger ( LogManager.GetLogger ( type ) );
		}
	}
}
